/*
 * send_sfd.c
 * Created on November 29, 2005; 5:53 PM
 * by vramadurai
 */

/************ System Header Files ***************/
#include <stdio.h>
#include <stdlib.h>

/************ Private Header Files **************/
#include "HsTypes.h"
#include "hs_types.h"
#include "PhyDebug.h"
#include "PhyStruct.h"
#include "PmdGlobals.h"
#include "PhyConstants.h"
#include "tables.h"
#include "tx_functions.h"
#include "wlan_phy_mac.h"
#include "iqdma_3500.h"

static inline hs_int32_t
wait_buffer(
				hs_int32_t subbuff_toggle,
				hs_int16_t **psTxOutDtoAPtrI,
				hs_int16_t **psTxOutDtoAPtrQ,
				hs_int16_t *psTxDtoAStartI,
				hs_int16_t *psTxDtoAStartQ
			)
{
	/** Wait till prev sub-buffer is Txmitted **/
   #ifndef PHY_RX_CYCLE_PRINT
	wait_sub_buffer_filled();
	reset_sub_buffer_interrupt();
   #endif

	subbuff_toggle ^= 1;

	if(subbuff_toggle){
		*psTxOutDtoAPtrI = psTxDtoAStartI + SUB_BUF_SIZE_TX;
		*psTxOutDtoAPtrQ = psTxDtoAStartQ + SUB_BUF_SIZE_TX;
	}
	else{
		*psTxOutDtoAPtrI = psTxDtoAStartI;
		*psTxOutDtoAPtrQ = psTxDtoAStartQ;
	}

	return subbuff_toggle;
}

void
send_sfd(
                struct HS_PhyTx11bStr  *HS_PhyTx11bPtrTh1,
                hs_int16_t  * restrict ssDBPSKTbl,
                ePHYSTATES_TH  *ePhyState,
                hs_int16_t * psTxDtoAStartI,
                hs_int16_t * psTxDtoAStartQ,
                hs_int16_t * HeaderSamplesI,
            	hs_int16_t * HeaderSamplesQ
             )
{
    hs_int32_t siBitCount;
    hs_int32_t offset;
    hs_int32_t subbuff_toggle;
    hs_int32_t symbol_count;
    hs_int32_t preamble_type;

    //int tic1, tic2, tic3, tic4;

    hs_int16_t * psTxOutDtoAPtrI;
    hs_int16_t * psTxOutDtoAPtrQ;

	preamble_type = HS_PhyTx11bPtrTh1->preamble_type;
    offset = HS_PhyTx11bPtrTh1->header_iq_offset;

	psTxOutDtoAPtrI = HS_PhyTx11bPtrTh1->psTxDtoABufferI;
    psTxOutDtoAPtrQ = HS_PhyTx11bPtrTh1->psTxDtoABufferQ;

    siBitCount = 0;
    subbuff_toggle = HS_PhyTx11bPtrTh1->subbuff_toggle;
    symbol_count = HS_PhyTx11bPtrTh1->symbol_count;

    while(1){

    	//tic1 = __sb_cfsr(MACH_CYC);
    	START_CLOCK
    	__sb_rcopy(psTxOutDtoAPtrI, &HeaderSamplesI[offset]);
    	__sb_rcopy(psTxOutDtoAPtrI+16, &HeaderSamplesI[offset+16]);
    	__sb_rcopy(psTxOutDtoAPtrQ, &HeaderSamplesQ[offset]);
    	__sb_rcopy(psTxOutDtoAPtrQ+16, &HeaderSamplesQ[offset+16]);

    	offset += 22;

        psTxOutDtoAPtrI += (22);
		psTxOutDtoAPtrQ += (22);

		symbol_count++;

        //tic2 = __sb_cfsr(MACH_CYC);

		if(symbol_count == 8){
			PRINT_CYCLES("send_sfd");
			symbol_count = 0;
			subbuff_toggle = wait_buffer(subbuff_toggle,
    									&psTxOutDtoAPtrI,
    									&psTxOutDtoAPtrQ,
    									psTxDtoAStartI,
    									psTxDtoAStartQ
								);
		}

        //tic3 = __sb_cfsr(MACH_CYC);

        siBitCount++;

        if(siBitCount == PLCP_SFD_BITS)
        {
            if(preamble_type == SHORT_PREAMBLE){
                if(HS_PhyTx11bPtrTh1->ucPrevSymb){
                  HS_PhyTx11bPtrTh1->ucPrevSymb = 2;
                }
                else{
                  HS_PhyTx11bPtrTh1->ucPrevSymb = 1;
                }
            }

            *ePhyState  = PHY_STATE_TX_SEND_HEADER_TH;

            //tic4 = __sb_cfsr(MACH_CYC);

            break;
        }
    }

	//printf("send_sfd: cyc = %d\n",  (tic2 - tic1) + (tic4 - tic3));

	HS_PhyTx11bPtrTh1->subbuff_toggle = subbuff_toggle;
	HS_PhyTx11bPtrTh1->symbol_count = symbol_count;
    HS_PhyTx11bPtrTh1->psTxDtoABufferI = psTxOutDtoAPtrI;
    HS_PhyTx11bPtrTh1->psTxDtoABufferQ = psTxOutDtoAPtrQ;
}
